/**
 * 
 */
package com.antilia.infomaresme.panels.filter;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.ResourceModel;
import org.odlabs.wiquery.core.IWiQueryPlugin;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.ui.commons.WiQueryUIPlugin;
import org.odlabs.wiquery.ui.tabs.Tabs;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.pages.CategoriesListPanel;
import com.antilia.infomaresme.pages.CategoryLinkPanel;
import com.antilia.infomaresme.pages.TopLevelLinkPanel;

/**
 * @author Ernesto
 *
 */
@WiQueryUIPlugin
public abstract class CategoryFilterPanel extends Panel implements IWiQueryPlugin {

	private static final long serialVersionUID = 1L;
	
	private Category selected;
	
	private Category place;
	
	private Tabs tabs;
	
	/**
	 * @param id
	 */
	public CategoryFilterPanel(String id) {
		super(id);
		setOutputMarkupId(true);		
	  	tabs = new Tabs("tabs");
	  	add(tabs);
	}
	
	@Override
	protected void onBeforeRender() {
		RepeatingView choosenCategory = new RepeatingView("choosenCategory");
		tabs.addOrReplace(choosenCategory);
		
		WebMarkupContainer wm  = new WebMarkupContainer(choosenCategory.newChildId());
			choosenCategory.add(wm);
			wm.add(new TopLevelLinkPanel("category",new ResourceModel("all")) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					selected = null;
					target.add(CategoryFilterPanel.this);	
					onCategorySelected(target, selected, place);
				}
			}
		);
			
		if(selected != null) {
			List<Category> parents = selected.getParentChain();
			for(Category category: parents) {
				wm  = new WebMarkupContainer(choosenCategory.newChildId());
				choosenCategory.add(wm);
				wm.add(new CategoryLinkPanel("category", category) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						selected = getCategory();
						target.add(CategoryFilterPanel.this);
						onCategorySelected(target, selected, place);
					}
				});
			}
			
			wm  = new WebMarkupContainer(choosenCategory.newChildId());
			choosenCategory.add(wm);
			wm.add(new CategoryLinkPanel("category", selected) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						selected = getCategory();
						target.add(CategoryFilterPanel.this);
						onCategorySelected(target, selected, place);
					}
				});
		} 
		
		
		RepeatingView choosenPlace = new RepeatingView("choosenPlace");
		tabs.addOrReplace(choosenPlace);
		
		wm  = new WebMarkupContainer(choosenPlace.newChildId());
			choosenPlace.add(wm);
			wm.add(new TopLevelLinkPanel("category",new ResourceModel("allM")) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					place = null;
					target.add(CategoryFilterPanel.this);	
					onCategorySelected(target, selected, place);
				}
			}
		);
			
		if(place != null) {
			List<Category> parents = place.getParentChain();
			for(Category category: parents) {
				wm  = new WebMarkupContainer(choosenPlace.newChildId());
				choosenPlace.add(wm);
				wm.add(new CategoryLinkPanel("category", category) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						place = getCategory();
						tabs.setDefaultSelectedTabIndex(1);
						target.add(CategoryFilterPanel.this);
						onCategorySelected(target, selected, place);
					}
				});
			}
			
			wm  = new WebMarkupContainer(choosenPlace.newChildId());
			choosenPlace.add(wm);
			wm.add(new CategoryLinkPanel("category", place) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						place = getCategory();
						tabs.setDefaultSelectedTabIndex(0);
						target.add(CategoryFilterPanel.this);
						onCategorySelected(target, selected, place);
					}
				});
		} 
		
		
		CategoriesListPanel categorias = new CategoriesListPanel("categorias", selected, new ResourceModel("chooseFilter")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				selected = getSelected();
				tabs.setDefaultSelectedTabIndex(0);
				target.add(CategoryFilterPanel.this);
				onCategorySelected(target, selected, place);
			}
			
			@Override
			protected List<Category> findTopLevel() {
				return InfomaresmeApplication.getQAApplication().getCategoriesDao().findTopLevel();
			}
		};
		
		tabs.addOrReplace(categorias);
				
		CategoriesListPanel places = new CategoriesListPanel("places", place, new ResourceModel("filterByPlace")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				place = getSelected();
				tabs.setDefaultSelectedTabIndex(1);
				target.add(CategoryFilterPanel.this);
				onCategorySelected(target, selected, place);
			}
			
			@Override
			protected List<Category> findTopLevel() {
				return InfomaresmeApplication.getQAApplication().getCategoriesDao().findTopLevelGeographic();
			}
		};
		
		tabs.addOrReplace(places);		
		super.onBeforeRender();
	}
	
	protected abstract void onCategorySelected(AjaxRequestTarget target, Category category, Category place);

	
	public Category getSelected() {
		return selected;
	}

	public void setSelected(Category selected) {
		this.selected = selected;
	}

	public JsStatement statement() {
		return null;
	}

	public Category getPlace() {
		return place;
	}

	public void setPlace(Category place) {
		this.place = place;
	}

}
